package com.canbot.u05.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.bean.ChooseFaceData;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.PathConst;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择人脸
 */

public class ChooseFaceAdapter extends BaseAdapter {

        private static final String TAG="ChooseFaceAdapter";
        private List<ChooseFaceData> data = new ArrayList<ChooseFaceData>();

        private Context context;

        private LayoutInflater inflater;


        public ChooseFaceAdapter(Context context, List<ChooseFaceData> objects) {
                this.context = context;
                this.inflater = LayoutInflater.from(context);
                this.data = objects;
        }

        @Override
        public int getCount() {
                return data.size();
        }

        @Override
        public ChooseFaceData getItem(int position) {
                return data.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {

                ViewHolder holder = null;
                if (convertView == null) {
                        holder = new ViewHolder();
                        convertView = inflater.inflate(R.layout.layout_choose_face_item, null);
                        holder.imageView = (ImageView) convertView.findViewById(R.id.item_iv_audio);
                        holder.fileName = (TextView) convertView.findViewById(R.id.shopping_item_tv_audio_num);
                        holder.checkBox = (CheckBox) convertView.findViewById(R.id.iv_shopping_mode_choose_audio);

                        convertView.setTag(holder);
                }
                else {
                        holder = (ViewHolder) convertView.getTag();
                }

                GlideApp.with(parent.getContext()).clear(holder.imageView);
                GlideApp.with(parent.getContext())
                        .asDrawable()
                        .load(IpMsgConst.HTTP_URL_CHEST + PathConst.FACES_ABS_DIR+data.get(position).getPath())
                        .thumbnail(Glide.with(parent.getContext()).load(IpMsgConst.HTTP_URL_CHEST + PathConst.FACES_THUMB_DIR+data.get(position).getPath()))
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                        .skipMemoryCache(false)
                        .placeholder(R.drawable.sales_banner01)//占位图
                        .error(R.drawable.ic_picture_loadfailed)
                        .override(100, 100)
                        .into(holder.imageView);


                ChooseFaceData faceData = data.get(position);
                Logger.e(TAG, "audio=" + faceData.toString());
                holder.fileName.setText(faceData.getName());
                String e = faceData.getName();
                Logger.e(TAG, "" + e);
                holder.checkBox.setChecked(faceData.isChecked());

                return convertView;

        }


        protected class ViewHolder {

                private ImageView imageView;

                private TextView fileName;

                private CheckBox checkBox;
        }

        /**
         * 更新控件显示
         *
         * @param posi
         * @param gridView
         */
        public void updateView(int posi, GridView gridView) {
                int visibleFirstPosi = gridView.getFirstVisiblePosition();
                int visibleLastPosi = gridView.getLastVisiblePosition();

                if (posi >= visibleFirstPosi && posi <= visibleLastPosi) {
                        View view = gridView.getChildAt(posi - visibleFirstPosi);
                        ViewHolder holder = (ViewHolder) view.getTag();
                        holder.checkBox.setChecked(data.get(posi).isChecked());
                }
                else {

                }
        }


}
